using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


namespace QuotesPlugin
{
    public partial class QuotesWidget : UserControl
    {
        public class QuotesEventArgs : EventArgs
        {
            public QuotesEventArgs(bool bChecked)
            {
                m_bChecked = bChecked;
            }
            public bool Checked
            {
                get { return m_bChecked; }
            }

            private bool m_bChecked;
        }
        public delegate void QuotesCheckedHandler (object sender, QuotesEventArgs e);
        public event QuotesCheckedHandler QuotesChecked;

        public QuotesWidget(IQuotesEntry entry)
        {
            InitializeComponent();
            
            m_entry = entry;
            this.Click += new EventHandler(QuotesWidget_Click);
            this.DoubleClick += new EventHandler(QuotesWidget_DoubleClick);
            this.MouseEnter += new EventHandler(QuotesWidget_MouseEnter);
            this.MouseLeave += new EventHandler(QuotesWidget_MouseLeave);
            this.MouseHover += new EventHandler(QuotesWidget_MouseHover);
        }

        void QuotesWidget_DoubleClick(object sender, EventArgs e)
        {
            string url = this.m_entry.QuoteUrl;
            if (String.IsNullOrEmpty(url))
                url = this.m_entry.AuthorUrl;

            if (!String.IsNullOrEmpty(url))
                System.Diagnostics.Process.Start(url);
        }

        void QuotesWidget_MouseHover(object sender, EventArgs e)
        {
            ToolTip tip = new ToolTip();
            tip.IsBalloon = true;
            
            string infor = "Double click to open it on web: " + m_entry.QuoteUrl;
            tip.SetToolTip(this, infor);
        }

        protected override Size DefaultSize
        {
            get { return new Size(1200, 50); }
        }

        void QuotesWidget_MouseLeave(object sender, EventArgs e)
        {
            m_hilight = false;
            this.Invalidate();
            this.Update();
        }

        void QuotesWidget_MouseEnter(object sender, EventArgs e)
        {
            m_hilight = true;
            this.Invalidate();
            this.Update();
        }
        void QuotesWidget_Click(object sender, EventArgs e)
        {
            this.m_checked = !this.m_checked;
            this.QuotesChecked(this, new QuotesEventArgs(this.m_checked));

            this.Invalidate();
            this.Update();
        }
        public bool Checked
        {
            get { return m_checked; }
        }
        public override string ToString()
        {
            StringBuilder htmlContent = new StringBuilder();
            htmlContent.Append("<table>");
            htmlContent.Append("<tr>");

            htmlContent.Append("<td>");
            htmlContent.AppendFormat("<img src=\"{0}\" width=\"{1}\" height=\"{2}\"></img>", 
                                this.m_entry.ImageUrl,
                                this.m_entry.ImageWidth,
                                this.m_entry.ImageHeight);
            htmlContent.Append("</td>");

            htmlContent.Append("<td>");
            htmlContent.Append("<B>");
            htmlContent.Append(this.m_entry.QuoteText);
            htmlContent.Append("</B>");
            htmlContent.Append("<p>");
            htmlContent.AppendFormat("<a href=\"{0}\">{1}</a>", this.m_entry.AuthorUrl, this.m_entry.Author);
            htmlContent.Append("</p>");
            htmlContent.Append("</td>");
            
            htmlContent.Append("</tr>");
            htmlContent.Append("</table>");

            htmlContent.Append("<p>");
            htmlContent.Append("Source: ");
            htmlContent.AppendFormat("<a href=\"{0}\">{0}</a>", this.m_entry.QuoteUrl);
            htmlContent.Append("</p>");
                        
            return htmlContent.ToString();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.DrawEntry(this.m_entry, e.Graphics, new Point(0,0));
        }
        
        private void DrawEntry(IQuotesEntry entry, Graphics g, Point pt)
        {
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);

            if (this.m_checked)
            {
                g.DrawRectangle(new Pen(Color.DarkOrange), rect);                
                g.FillRectangle(new SolidBrush(Color.Yellow), rect);
            }
            if (this.m_hilight)
            {
                g.DrawRectangle(new Pen(Color.Pink), rect);
                g.FillRectangle(new SolidBrush(Color.LimeGreen), rect);
            }

            const int width = 40;

            Image image = entry.QuoteImage;
            double ratio = 1.00 * image.Height / image.Width;
            int height = Convert.ToInt32(ratio * width);
            g.DrawImage(image, new Rectangle(pt, new Size(width, height)));

            Font font = new Font(this.Font.FontFamily, 12);

            g.DrawString(entry.QuoteText,
                            font,
                            new SolidBrush(Color.OrangeRed),
                            pt.X + width + 10,
                            pt.Y);

            string authorInfo = entry.Author + ", ";
            authorInfo += entry.AuthorFrom + " - " + entry.AuthorTo + ", ";
            authorInfo += entry.AuthorDescription;
            g.DrawString(authorInfo,
                            new Font(font.FontFamily, 9),
                            new SolidBrush(Color.Blue),
                            pt.X + width + 30,
                            pt.Y + font.Height);
        }

        private int Max(int a, int b)
        {
            return (a > b) ? a : b;
        }

        #region "PRIVATE DATA MEMBERS"
        private IQuotesEntry m_entry;
        private bool m_checked = false;
        private bool m_hilight = false;
        #endregion
    }
}
